package com.xiaochong.tir.manager.service;


import com.xiaochong.tir.manager.config.druid.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaochong.tir.common.data.mapper.AccountMapper;
import com.xiaochong.tir.common.data.po.Account;
import com.xiaochong.tir.manager.util.EncodeUtils;


/**
 * @author yss
 *
 */
@Service
public class AccountService {

    @Autowired
    private AccountMapper accountMapper;

    
    public Account loginBack(String userName, String password) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
            return null;
        }
        Account account = getAccountByUserName(userName);
        password = EncodeUtils.md5Encode(password);
        if (account == null || !password.equals(account.getPassword())){
            return null;
        }
        return account;
    }

    
    public Account getAccountByUserName(String userName) {
        Account account = new Account();
        account.setUserName(userName);
        return accountMapper.selectByEntity(account);
    }

    
    public Account getAccountById(Integer id) {
        if(null == id) return  null;
        Account account = new Account();
        account.setId(id);
        return accountMapper.selectByEntity(account);
    }

    @Operation
    @Transactional
    public int insertAccount(Account account) {
        return accountMapper.insertSelective(account);
    }

    @Operation
    @Transactional
    public int updateAccount(Account account,Account old){
        return accountMapper.updateByPrimaryKey(account);
    }

    @Transactional
    public int editAccount(Account account){
        return accountMapper.updateByPrimaryKey(account);
    }

    
    public PageInfo<Account> getAccountList(Integer status, String find, String startTime, String endTime, int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<Account>(accountMapper.getAccountList(status, find, startTime, endTime));
    }

    public Account selectByPrimaryKey(Integer id) {
       return accountMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据条件查询单条记录
     * @param entity
     */
    public Account selectByEntity(Account entity) {
        return this.accountMapper.selectByEntity(entity);
    }
}
